import NProgress from 'nprogress';
import useUserStore from '@/stores/user';
import cookie from '@/api/cookie';
import router from './index';
import 'nprogress/nprogress.css';

NProgress.configure({ showSpinner: false });

router.beforeEach(async (to, from, next) => {
  NProgress.start();

  const hasToken = cookie.getToken();
  if (hasToken) {
    try {
      const user = useUserStore();
      user.getUserInfo();
    } catch (error) { console.warn(error); }
    next();
    return;
  }

  next();
});

router.afterEach(() => {
  NProgress.done();
});
